#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 300010;
long long a[maxn];
long long b[maxn];
long long c[maxn];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		//printf("-----\n");
		long long ans = 0;
		long long mmi = 1e12+10;
		for(int i = 0;i<n;i++)
		{
			scanf("%lld",&a[i]);
			scanf("%lld",&b[i]);
		//	printf("****\n");
		}
		if(b[n-1]<a[0])
		{
			ans += a[0]-b[n-1];
			c[0] = b[n-1];
		}
		else c[0] = a[0];
		for(int i = 1;i<n;i++)
		{
			if(b[i-1]<a[i])
			{
				ans += a[i]-b[i-1];
				c[i] = b[i-1];
			}
			else c[i] = a[i];
		}
		for(int i = 0;i<n;i++)
		{
			if(c[i]<mmi) mmi  = c[i];
		}
		printf("%lld\n",mmi+ans);
	}
}
